home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / RTrace-1.0-src / defs.h < prev    next >
Text File  |  1992-09-04  |  19KB  |  860 lines

  1. /*
  2.  * Copyright (c) 1988, 1992 Antonio Costa, INESC-Norte.
  3.  * All rights reserved.
  4.  *
  5.  * This code received contributions from the following people:
  6.  *
  7.  *  Roman Kuchkuda      - basic ray tracer
  8.  *  Mark VandeWettering - MTV ray tracer
  9.  *  Augusto Sousa       - overall, shading model
  10.  *
  11.  * Redistribution and use in source and binary forms are permitted
  12.  * provided that the above copyright notice and this paragraph are
  13.  * duplicated in all such forms and that any documentation,
  14.  * advertising materials, and other materials related to such
  15.  * distribution and use acknowledge that the software was developed
  16.  * by Antonio Costa, at INESC-Norte. The name of the author and
  17.  * INESC-Norte may not be used to endorse or promote products derived
  18.  * from this software without specific prior written permission.
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  */
  23. #include "config.h"
  24.  
  25. /**********************************************************************
  26.  *    RAY TRACING - Defs and Typedefs - Version 7.3.1                 *
  27.  *                                                                    *
  28.  *    MADE BY    : Antonio Costa, INESC-Norte, October 1988           *
  29.  *    ADAPTED BY : Antonio Costa, INESC-Norte, June 1989              *
  30.  *    MODIFIED BY: Antonio Costa, INESC-Norte, July 1992              *
  31.  **********************************************************************/
  32.  
  33. #define STRING_MAX (255)
  34.  
  35. #define ROUNDOFF ((real) 1.0e-12)
  36. #define INFINITY ((real) 1.0e+20)
  37.  
  38. #ifndef PI
  39. #ifdef M_PI
  40. #define PI ((real) M_PI)
  41. #else
  42. #define PI ((real) 3.1415926535897932)
  43. #endif
  44. #endif
  45.  
  46. /* Minimum angle between Gaze and Up (5 degrees) */
  47. #define ANGLE_MIN ((real) 5.0 * PI / 180.0)
  48.  
  49. /***** Ray Tracing constants *****/
  50. #ifdef THINK_C
  51.  
  52. /* On the Macintosh, these maximums are not constants but variables.
  53.     Before each rendering, the image size and number of objects is
  54.     computed, and the values are set accordingly */
  55.  
  56. extern short    SCREEN_SIZE_X_MAX;
  57. extern short    SCREEN_SIZE_Y_MAX;
  58. extern short    OBJECTS_MAX;
  59.  
  60. #define RAY_CACHE_LEVEL_MAX   (12)
  61.  
  62. #else
  63. #define SCREEN_SIZE_X_MAX (2049)/* Screen width maximum + 1  */
  64. #define SCREEN_SIZE_Y_MAX (2049)/* Screen height maximum + 1 */
  65. #define OBJECTS_MAX  (25000)
  66. #define RAY_CACHE_LEVEL_MAX   (12)
  67.  
  68. #endif
  69.  
  70. #define INDEX_MAX (255)
  71.  
  72. #define X_MAX ((real) 100000.0)
  73. #define X_MIN (-X_MAX)
  74. #define Y_MAX (X_MAX)
  75. #define Y_MIN (X_MIN)
  76. #define Z_MAX (X_MAX)
  77. #define Z_MIN (X_MIN)
  78. #define W_MAX (X_MAX)
  79. #define W_MIN (X_MIN)
  80.  
  81. #define LIGHTING_FACTOR_MAX   ((real) 300.0)
  82. #define SPECULAR_FACTOR_MAX   ((real) 300.0)
  83. #define REFRACTION_FACTOR_MAX ((real) 300.0)
  84. #define DISTANCE_FACTOR       ((real) 1.0e-4)
  85.  
  86. #define LIGHTS_MAX   (12)
  87. #define SURFACES_MAX (512)
  88.  
  89. #define CSG_LEVEL_MAX  (256)
  90. #define LIST_LEVEL_MAX (1)
  91.  
  92. #define LIGHT_CACHE_LEVEL_MAX (12)
  93.  
  94. #define RAY_SIZE_MAX ((1 SHL RAY_CACHE_LEVEL_MAX) - 1)
  95.  
  96. #define NO_OBJECTS (0)
  97.  
  98. #define SCALE_MIN ((real) 0.0)
  99. #define SCALE_MAX ((real) MAXINT)
  100.  
  101. #define CLUSTER_SIZE_MIN (2)    /* Cluster size minimum - sparse */
  102. #define CLUSTER_SIZE_MAX (16)   /* Cluster size maximum - dense  */
  103.  
  104. #define PQUEUE_SIZE_MAX (256)
  105.  
  106. #define SAMPLING_LEVEL_MAX (3)  /* Pixel supersampling level maximum */
  107.  
  108. #define X_AXIS (1)
  109. #define Y_AXIS (2)
  110. #define Z_AXIS (3)
  111.  
  112. #define STEREO_LEFT  (1)
  113. #define STEREO_RIGHT (2)
  114.  
  115. #define ROOT_OBJECT object[0]
  116.  
  117. /***** Typedefs *****/
  118. typedef
  119. real            array1[3];
  120.  
  121. typedef
  122. real            array2[3][3];
  123.  
  124. /***** Ray Tracing types *****/
  125. typedef
  126. struct
  127. {
  128.   real            x, y, z;
  129. } xyz_struct;
  130. typedef
  131. xyz_struct     *xyz_ptr;
  132.  
  133. typedef
  134. struct
  135. {
  136.   real            x, y, z, w;
  137. } xyzw_struct;
  138. typedef
  139. xyzw_struct    *xyzw_ptr;
  140.  
  141. typedef
  142. struct
  143. {
  144.   real            r, g, b;
  145. } rgb_struct;
  146. typedef
  147. rgb_struct     *rgb_ptr;
  148.  
  149. typedef
  150. struct
  151. {
  152.   int             id;
  153.   rgb_struct      color;
  154. } pixel_struct;
  155. typedef
  156. pixel_struct   *pixel_ptr;
  157.  
  158. typedef
  159. struct
  160. {
  161.   xyz_struct      vector;
  162.   rgb_struct      level;
  163.   boolean         inside;
  164. } ray_struct;
  165. typedef
  166. ray_struct     *ray_ptr;
  167.  
  168. typedef
  169. struct
  170. {
  171.   rgb_struct      color, diffuse, specular, transparent;
  172.   real            phong_factor;
  173.   rgb_struct      diffuse_factor, metal_factor;
  174. } surface_struct;
  175. typedef
  176. surface_struct *surface_ptr;
  177.  
  178. #define SURFACE_TYPE1     (1)   /* Original format: C Kd Ks P M Kt */
  179. #define SURFACE_TYPE2     (2)   /* Paul Strauss format: C S M Kt   */
  180.  
  181. #define SURFACE_KINDS_MAX (2)
  182.  
  183. /***** Textures *****/
  184. #define NULL_TYPE      (0)
  185. #define CHECKER_TYPE   (1)      /* Checkerboard   */
  186. #define BLOTCH_TYPE    (2)      /* Color blotches */
  187. #define BUMP_TYPE      (3)      /* Bump mapping   */
  188. #define MARBLE_TYPE    (4)      /* Marble texture */
  189. #define FBM_TYPE       (5)      /* fBm texture    */
  190. #define FBM_BUMP_TYPE  (6)      /* fBm bump map   */
  191. #define WOOD_TYPE      (7)      /* Wood           */
  192. #define ROUND_TYPE     (8)      /* Round          */
  193. #define BOZO_TYPE      (9)      /* Bozo           */
  194. #define RIPPLES_TYPE   (10)     /* Ripples        */
  195. #define WAVES_TYPE     (11)     /* Sea waves      */
  196. #define SPOTTED_TYPE   (12)     /* Spotts         */
  197. #define DENTS_TYPE     (13)     /* Dents          */
  198. #define AGATE_TYPE     (14)     /* Agate          */
  199. #define WRINKLES_TYPE  (15)     /* Wrinkles       */
  200. #define GRANITE_TYPE   (16)     /* Granite        */
  201. #define GRADIENT_TYPE  (17)     /* Color gradient */
  202. #define IMAGE_MAP_TYPE (18)     /* Image mapping  */
  203. #define GLOSS_TYPE     (19)     /* Gloss          */
  204. #define BUMP3_TYPE     (20)    /* Bump 3d        */
  205.  
  206. #define TEXTURE_KINDS_MAX (20)
  207.  
  208. #define WAVES_MAX (10)
  209. typedef
  210. struct
  211. {
  212.   xyz_struct      source[WAVES_MAX];
  213.   real            frequency[WAVES_MAX];
  214. } wave_struct;
  215.  
  216. typedef
  217. struct
  218. {
  219.   short int       surface_id;
  220. } checker_struct;
  221. typedef
  222. checker_struct *checker_ptr;
  223.  
  224. typedef
  225. struct
  226. {
  227.   short int       surface_id;
  228.   real            scale;
  229. } blotch_struct;
  230. typedef
  231. blotch_struct  *blotch_ptr;
  232.  
  233. typedef
  234. struct
  235. {
  236.   real            scale;
  237. } bump_struct;
  238. typedef
  239. bump_struct    *bump_ptr;
  240.  
  241. typedef
  242. struct
  243. {
  244.   rgb_ptr         color;
  245. } marble_struct;
  246. typedef
  247. marble_struct  *marble_ptr;
  248.  
  249. typedef
  250. struct
  251. {
  252.   real            offset, scale, omega, lambda, threshold;
  253.   int             octaves;
  254.   rgb_ptr         color;
  255. } fbm_struct;
  256. typedef
  257. fbm_struct     *fbm_ptr;
  258.  
  259. typedef
  260. struct
  261. {
  262.   real            offset, scale, omega, lambda;
  263.   int             octaves;
  264. } fbm_bump_struct;
  265. typedef
  266. fbm_bump_struct *fbm_bump_ptr;
  267.  
  268. typedef
  269. struct
  270. {
  271.   rgb_struct      color;
  272. } wood_struct;
  273. typedef
  274. wood_struct    *wood_ptr;
  275.  
  276. typedef
  277. struct
  278. {
  279.   real            scale;
  280. } round_struct;
  281. typedef
  282. round_struct   *round_ptr;
  283.  
  284. typedef
  285. struct
  286. {
  287.   real            turbulence;
  288.   rgb_ptr         color;
  289. } bozo_struct;
  290. typedef
  291. bozo_struct    *bozo_ptr;
  292.  
  293. typedef
  294. struct
  295. {
  296.   real            frequency, phase, scale;
  297. } ripples_struct;
  298. typedef
  299. ripples_struct *ripples_ptr;
  300.  
  301. typedef
  302. struct
  303. {
  304.   real            frequency, phase, scale;
  305. } waves_struct;
  306. typedef
  307. waves_struct   *waves_ptr;
  308.  
  309. typedef
  310. struct
  311. {
  312.   rgb_ptr         color;
  313. } spotted_struct;
  314. typedef
  315. spotted_struct *spotted_ptr;
  316.  
  317. typedef
  318. struct
  319. {
  320.   real            scale;
  321. } dents_struct;
  322. typedef
  323. dents_struct   *dents_ptr;
  324.  
  325. typedef
  326. struct
  327. {
  328.   rgb_ptr         color;
  329. } agate_struct;
  330. typedef
  331. agate_struct   *agate_ptr;
  332.  
  333. typedef
  334. struct
  335. {
  336.   real            scale;
  337. } wrinkles_struct;
  338. typedef
  339. wrinkles_struct *wrinkles_ptr;
  340.  
  341. typedef
  342. struct
  343. {
  344.   rgb_ptr         color;
  345. } granite_struct;
  346. typedef
  347. granite_struct *granite_ptr;
  348.  
  349. typedef
  350. struct
  351. {
  352.   real            turbulence;
  353.   xyz_struct      direction;
  354.   rgb_ptr         color;
  355. } gradient_struct;
  356. typedef
  357. gradient_struct *gradient_ptr;
  358.  
  359. typedef
  360. struct
  361. {
  362.   real            turbulence;
  363.   boolean         once;
  364.   unsigned short int width, height;
  365.   short int       u, v;
  366.   file_ptr        image;
  367. } image_map_struct;
  368. typedef
  369. image_map_struct *image_map_ptr;
  370.  
  371. typedef
  372. struct
  373. {
  374.   real            scale;
  375. } gloss_struct;
  376. typedef
  377. gloss_struct   *gloss_ptr;
  378.  
  379. typedef
  380. struct
  381. {
  382.   real            scale;
  383.   real            size;
  384. } bump3_struct;
  385. typedef
  386. bump3_struct   *bump3_ptr;
  387.  
  388. typedef
  389. struct
  390. {
  391.   short int       type;
  392.   xyzw_ptr        transf;
  393.   void_ptr        next;
  394.   void_ptr        data;
  395. } texture_struct;
  396. typedef
  397. texture_struct *texture_ptr;
  398.  
  399. typedef
  400. struct
  401. {
  402.   xyz_struct      center;
  403.   real            radius, radius2;
  404.   xyz_ptr         position;
  405. } sphere_struct;
  406. typedef
  407. sphere_struct  *sphere_ptr;
  408.  
  409. typedef
  410. struct
  411. {
  412.   xyz_struct      center, size;
  413.   short int       side_hit;
  414.   xyz_ptr         position;
  415. } box_struct;
  416. typedef
  417. box_struct     *box_ptr;
  418.  
  419. typedef
  420. struct
  421. {
  422.   xyz_struct      coords;
  423.   void_ptr        next;
  424. } vertex_struct;
  425. typedef
  426. vertex_struct  *vertex_ptr;
  427.  
  428. typedef
  429. struct
  430. {
  431.   vertex_ptr      vertex;
  432.   vertex_ptr      p[12];
  433.   xyz_struct      c[4][4];
  434.   real            u_hit, v_hit;
  435. } patch_struct;
  436. typedef
  437. patch_struct   *patch_ptr;
  438.  
  439. typedef
  440. struct
  441. {
  442.   xyz_struct      base, apex, u, v, w;
  443.   real            base_radius, base_d, apex_radius;
  444.   real            height, slope, min_d, max_d;
  445.   xyz_ptr         position;
  446. } cone_struct;
  447. typedef
  448. cone_struct    *cone_ptr;
  449.  
  450. typedef
  451. struct
  452. {
  453.   short int       points;
  454.   array1         *coords;
  455.   xyz_struct      normal;
  456.   real            d;
  457.   short int       u, v;
  458. } polygon_struct;
  459. typedef
  460. polygon_struct *polygon_ptr;
  461.  
  462. typedef
  463. struct
  464. {
  465.   xyz_struct      coords[3], normal[3], transf[3];
  466.   real            u_hit, v_hit;
  467. } triangle_struct;
  468. typedef
  469. triangle_struct *triangle_ptr;
  470.  
  471. typedef
  472. struct
  473. {
  474.   xyz_ptr         position;
  475.   void_ptr        data;
  476. } text_struct;
  477. typedef
  478. text_struct    *text_ptr;
  479.  
  480. typedef
  481. struct
  482. {
  483.   unsigned char   op;
  484.   xyz_ptr         position;
  485.   short int       surface_id;
  486.   real            refraction;
  487.   texture_ptr     texture;
  488.   int             left, right;
  489. } csg_struct;
  490. typedef
  491. csg_struct     *csg_ptr;
  492.  
  493. typedef
  494. struct
  495. {
  496.   short int       size;
  497.   void_ptr       *object;
  498. } cluster_struct;
  499. typedef
  500. cluster_struct *cluster_ptr;
  501.  
  502. /***** Objects *****/
  503. #define CLUSTER_TYPE  (0)       /* Cluster            */
  504. #define SPHERE_TYPE   (1)       /* Spheres            */
  505. #define BOX_TYPE      (2)       /* Axis aligned Boxes */
  506. #define PATCH_TYPE    (3)       /* Bicubic Patches    */
  507. #define CONE_TYPE     (4)       /* Cones/Cylinders    */
  508. #define POLYGON_TYPE  (5)       /* Polygons           */
  509. #define TRIANGLE_TYPE (6)       /* Triangular Patches */
  510. #define TEXT_TYPE     (7)       /* Text PP Primitive  */
  511. #define CSG_TYPE      (8)       /* CSG Tree           */
  512. #define LIST_TYPE     (9)    /* List of Objects    */
  513.  
  514. #define OBJECT_KINDS_MAX (9)
  515.  
  516. #define TEXTURE_TYPE  (64)      /* Texture        */
  517. #define TRANSF_TYPE   (65)      /* Transformation */
  518. #define CSG_OP_TYPE   (66)      /* CSG            */
  519. #define LIST_OP_TYPE  (67)    /* List           */
  520.  
  521. #define CSG_UNION        (0)
  522. #define CSG_SUBTRACTION  (1)
  523. #define CSG_INTERSECTION (2)
  524.  
  525. #define LIST_BEGIN (0)
  526. #define LIST_END   (1)
  527.  
  528. typedef
  529. struct
  530. {
  531.   int             id;           /* Object identifier  */
  532.   short int       surface_id;   /* Surface identifier */
  533.   real            refraction;   /* Refraction index   */
  534.   xyz_ptr         min, max;     /* Bounding volume    */
  535.   xyzw_ptr        transf;       /* Transformation     */
  536.   xyzw_ptr        inv_transf;   /* Inverse transf.    */
  537.   texture_ptr     texture;      /* List of textures   */
  538.   boolean         texture_modify_normal;
  539.   unsigned char   object_type;
  540.   void_ptr        data;
  541. } object_struct;
  542. typedef
  543. object_struct  *object_ptr;
  544.  
  545. typedef 
  546. struct
  547. {
  548.   object_ptr      object;
  549.   void_ptr        next;
  550. } list_node_struct;
  551. typedef
  552. list_node_struct *list_node_ptr;
  553.  
  554. typedef
  555. struct
  556. {
  557.   xyz_ptr         position;
  558.   short int       surface_id;
  559.   real            refraction;
  560.   object_ptr      object;
  561.   list_node_ptr   head;
  562. } list_struct;
  563. typedef
  564. list_struct    *list_ptr;
  565.  
  566. /***** Lights *****/
  567. #define POINT_LIGHT_TYPE    (1) /* Point light            */
  568. #define DIRECT_LIGHT_TYPE   (2) /* Directional/Spot light */
  569. #define EXTENDED_LIGHT_TYPE (3) /* Extended light         */
  570.  
  571. #define LIGHT_KINDS_MAX (3)
  572.  
  573. typedef
  574. struct
  575. {
  576.   xyz_struct      coords;
  577.   rgb_struct      brightness;
  578.   boolean         attenuation[3];
  579.   int             cache_id[LIGHT_CACHE_LEVEL_MAX];
  580.   short int       light_type;
  581.   void_ptr        data;
  582. } light_struct;
  583.  
  584. typedef
  585. struct
  586. {
  587.   xyz_struct      vector;
  588.   real            cos_angle, t, factor;
  589. } dir_light_struct;
  590. typedef
  591. dir_light_struct *dir_light_ptr;
  592.  
  593. typedef
  594. struct
  595. {
  596.   real            diameter;
  597.   short int       samples;
  598. } ext_light_struct;
  599. typedef
  600. ext_light_struct *ext_light_ptr;
  601.  
  602. /***** Others *****/
  603. typedef
  604. struct
  605. {
  606.   real            key;
  607.   object_ptr      object;
  608. } pqueue_struct;
  609.  
  610. typedef
  611. int             list_id[4];
  612.  
  613. /***** General Macros *****/
  614. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  615. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  616.  
  617. #define REALINC(x) ((x) += 1.0)
  618.  
  619. #define DEGREE_TO_RADIAN(angle) ((real) (angle) * PI / 180.0)
  620.  
  621. #define PQUEUE_INITIALIZE \
  622. do {\
  623.   pqueue_size = 0;\
  624.   REALINC(pqueue_resets);\
  625. } while (0)
  626. #define PQUEUE_NOT_EMPTY ((boolean) (pqueue_size > 0))
  627.  
  628. #define DOT_PRODUCT(p0, p1)\
  629. ((p0).x * (p1).x + (p0).y * (p1).y + (p0).z * (p1).z)
  630.  
  631. #define LENGTH(p) (SQRT(DOT_PRODUCT(p, p)))
  632. #define NORMALIZE(p)\
  633. do {\
  634.   REG real        d_TEMP;\
  635. \
  636.   d_TEMP = LENGTH(p);\
  637.   if (d_TEMP < ROUNDOFF)\
  638.     runtime_abort("cannot normalize a NULL VECTOR");\
  639.   d_TEMP = 1.0 / d_TEMP;\
  640.   (p).x *= d_TEMP;\
  641.   (p).y *= d_TEMP;\
  642.   (p).z *= d_TEMP;\
  643. } while (0)
  644.  
  645. #define CROSS_PRODUCT(p, p0, p1)\
  646. do {\
  647.   (p).x = (p0).y * (p1).z - (p0).z * (p1).y;\
  648.   (p).y = (p0).z * (p1).x - (p0).x * (p1).z;\
  649.   (p).z = (p0).x * (p1).y - (p0).y * (p1).x;\
  650. } while (0)
  651.  
  652. #define CHECK_BOUNDS(type)\
  653. (((type) != SPHERE_TYPE) AND((type) != BOX_TYPE))
  654.  
  655. #define OBJECT_INTERSECT(distance, position, vector, object)\
  656. do {\
  657.   xyz_struct      pos_TEMP;\
  658. \
  659.   if (intersect_adjust_mode)\
  660.   {\
  661.     pos_TEMP.x = (position)->x + threshold_distance * (vector)->x;\
  662.     pos_TEMP.y = (position)->y + threshold_distance * (vector)->y;\
  663.     pos_TEMP.z = (position)->z + threshold_distance * (vector)->z;\
  664.   } else\
  665.     STRUCT_ASSIGN(pos_TEMP, *(position));\
  666.   switch ((object)->object_type)\
  667.   {\
  668.   case SPHERE_TYPE:\
  669.     distance = sphere_intersect(&pos_TEMP, vector, object);\
  670.     break;\
  671.   case BOX_TYPE:\
  672.     distance = box_intersect(&pos_TEMP, vector, object);\
  673.     break;\
  674.   case PATCH_TYPE:\
  675.     distance = patch_intersect(&pos_TEMP, vector, object);\
  676.     break;\
  677.   case CONE_TYPE:\
  678.     distance = cone_intersect(&pos_TEMP, vector, object);\
  679.     break;\
  680.   case POLYGON_TYPE:\
  681.     distance = polygon_intersect(&pos_TEMP, vector, object);\
  682.     break;\
  683.   case TRIANGLE_TYPE:\
  684.     distance = triangle_intersect(&pos_TEMP, vector, object);\
  685.     break;\
  686.   case TEXT_TYPE:\
  687.     distance = text_intersect(&pos_TEMP, vector, object);\
  688.     break;\
  689.   case CSG_TYPE:\
  690.     distance = csg_intersect(&pos_TEMP, vector, object);\
  691.     break;\
  692.   case LIST_TYPE:\
  693.     distance = list_intersect(&pos_TEMP, vector, object);\
  694.     break;\
  695.   }\
  696.   if (intersect_adjust_mode AND(distance != 0.0))\
  697.     distance += threshold_distance;\
  698. } while (0)
  699.  
  700. #define OBJECT_NORMAL(position, object, normal)\
  701. switch ((object)->object_type)\
  702. {\
  703. case SPHERE_TYPE:\
  704.   sphere_normal(position, object, normal);\
  705.   break;\
  706. case BOX_TYPE:\
  707.   box_normal(position, object, normal);\
  708.   break;\
  709. case PATCH_TYPE:\
  710.   patch_normal(position, object, normal);\
  711.   break;\
  712. case CONE_TYPE:\
  713.   cone_normal(position, object, normal);\
  714.   break;\
  715. case POLYGON_TYPE:\
  716.   polygon_normal(position, object, normal);\
  717.   break;\
  718. case TRIANGLE_TYPE:\
  719.   triangle_normal(position, object, normal);\
  720.   break;\
  721. case TEXT_TYPE:\
  722.   text_normal(position, object, normal);\
  723.   break;\
  724. case CSG_TYPE:\
  725.   csg_normal(position, object, normal);\
  726.   break;\
  727. case LIST_TYPE:\
  728.   list_normal(position, object, normal);\
  729.   break;\
  730. }
  731.  
  732. #define ATTEN_DISTANCE(distance, factor)\
  733. factor = gaze_distance / (gaze_distance + (distance) * 0.25)
  734.  
  735. #define ATTEN_COLOR(distance, atten_r, atten_g, atten_b, old, new)\
  736. do {\
  737.   REG real        k_TEMP;\
  738. \
  739.   ATTEN_DISTANCE(distance, k_TEMP);\
  740.   (new).r = atten_r ? k_TEMP * (old).r : (old).r;\
  741.   (new).g = atten_g ? k_TEMP * (old).g : (old).g;\
  742.   (new).b = atten_b ? k_TEMP * (old).b : (old).b;\
  743. } while (0)
  744.  
  745. #define FIND_OCTANT(octant, vector)\
  746. do {\
  747.   (octant) = (vector).x >= 0.0 ? 1 : 0;\
  748.   if ((vector).y >= 0.0)\
  749.     (octant) += 2;\
  750.   if ((vector).z >= 0.0)\
  751.     (octant) += 4;\
  752. } while (0)
  753.  
  754. #define JITTER (RANDOM - 0.5)
  755.  
  756. #define ALLOCATE(p, s, c)\
  757. do {\
  758.   ALLOC(p, s, c);\
  759.   if (NOT(p))\
  760.     {\
  761.     WRITE(results, "Error: cannot allocate MEMORY\n");\
  762.     HALT;\
  763.     }\
  764. } while (0)
  765.  
  766. #define ADVANCE(file)\
  767. do {\
  768.   READ_LINE(file)\
  769.   while (PEEK_CHAR(file) == '#')\
  770.     READ_LINE(file)\
  771.   if (IO_status != IO_OK)\
  772.   {\
  773.     WRITE(results, "Error: bad INPUT FORMAT\n");\
  774.     HALT;\
  775.   }\
  776. } while (0)
  777.  
  778. #define MODIFY_NORMAL(type)\
  779. (((type) == BUMP_TYPE)\
  780.  OR((type) == FBM_BUMP_TYPE)\
  781.  OR((type) == ROUND_TYPE)\
  782.  OR((type) == RIPPLES_TYPE)\
  783.  OR((type) == WAVES_TYPE)\
  784.  OR((type) == DENTS_TYPE)\
  785.  OR((type) == WRINKLES_TYPE)\
  786.  OR((type) == BUMP3_TYPE))
  787.  
  788. #define CLOSED(type)\
  789. (((type) == SPHERE_TYPE)\
  790.  OR((type) == BOX_TYPE)\
  791.  OR((type) == TEXT_TYPE)\
  792.  OR((type) == CSG_TYPE))
  793.  
  794. #define CONVEX(type)\
  795. (((type) != PATCH_TYPE)\
  796.  AND((type) != TEXT_TYPE)\
  797.  AND((type) != CSG_TYPE)\
  798.  AND((type) != LIST_TYPE))
  799.  
  800. #define SELF_INTERSECT(type, inside)\
  801. ((inside) ?\
  802. (((type) == SPHERE_TYPE)\
  803.  OR((type) == BOX_TYPE)\
  804.  OR((type) == CONE_TYPE)\
  805.  OR((type) == PATCH_TYPE)\
  806.  OR((type) == TEXT_TYPE)\
  807.  OR((type) == CSG_TYPE)\
  808.  OR((type) == LIST_TYPE)) :\
  809. (((type) == PATCH_TYPE)\
  810.  OR((type) == TEXT_TYPE)\
  811.  OR((type) == CSG_TYPE)\
  812.  OR((type) == LIST_TYPE)))
  813.  
  814. #define UNIT_CIRCLE_POINT(x, y)\
  815. do {\
  816.   x = 2.0 * RANDOM - 1.0;\
  817.   y = SQRT(1.0 - SQR(x)) * (2.0 * RANDOM - 1.0);\
  818. } while (0)
  819.  
  820. #define COLOR_BIG(color, value)\
  821. (((color).r > (value)) OR((color).g > (value)) OR((color).b > (value)))
  822.  
  823. #define OBJECT_ENCLOSE(object)\
  824. do {\
  825.   if (object->transf != NULL)\
  826.   {\
  827.     object->inv_transf = object->transf;\
  828.     ALLOCATE(object->transf, xyzw_struct, 4);\
  829.     inverse_transform(object->inv_transf, object->transf);\
  830.   }\
  831.   switch ((object)->object_type)\
  832.   {\
  833.   case SPHERE_TYPE:\
  834.     sphere_enclose(object);\
  835.     break;\
  836.   case BOX_TYPE:\
  837.     box_enclose(object);\
  838.     break;\
  839.   case PATCH_TYPE:\
  840.     patch_enclose(object);\
  841.     break;\
  842.   case CONE_TYPE:\
  843.     cone_enclose(object);\
  844.     break;\
  845.   case POLYGON_TYPE:\
  846.     polygon_enclose(object);\
  847.     break;\
  848.   case TRIANGLE_TYPE:\
  849.     triangle_enclose(object);\
  850.     break;\
  851.   case TEXT_TYPE:\
  852.     text_enclose(object);\
  853.     break;\
  854.   case CSG_TYPE:\
  855.     break;\
  856.   case LIST_TYPE:\
  857.     list_enclose(object);\
  858.     break;\
  859.   }\
  860. } while (0)